//
//  HXMeetingRcachListTableViewController.m
//  KD
//
//  Created by ifly on 16/5/26.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXMeetingRcachListTableViewController.h"
#import "HXReachTableViewCell.h"

@interface HXMeetingRcachListTableViewController ()
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray<HXReachListModel *> *dataArray ;
@end

@implementation HXMeetingRcachListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 81;
    self.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView.mj_header beginRefreshing];
}

-(void)refreshData{
    self.page = 0;
    [NetworkAPI getReachDetailsWithMessID:self.ID
                           success:^(HXReachModel *result) {
                               if ([result.code isEqualToString:@"200"]) {
                                   [self.tableView.mj_header endRefreshing];
                                   self.page++;
                                   [self.dataArray removeAllObjects];
                                   [self.dataArray addObjectsFromArray:result.msglist];
                                   [self.tableView reloadData];
                                   if ([result.msglist count] < 10) {
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                       return;
                                   }
                               }else if([result.code isEqualToString:@"400"]){
                                   [self.tableView.mj_header endRefreshing];
                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                   self.tableView.mj_footer.alpha = 1.0f;
                               }else{
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   [self.tableView.mj_header endRefreshing];
                               }
                           } failure:^(NSError *err) {
                               [self.tableView.mj_header endRefreshing];
                           }];
}
-(void)loadMoreData{
    [NetworkAPI getReachDetailsWithMessID:self.ID
                                  success:^(HXReachModel *result) {
                               if ([result.code isEqualToString:@"200"]) {
                                   if ([result.msglist count] == 0) {//如果没有数据了
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                       return;
                                   }else if([result.msglist count] < 10){//有数据但是没有下一页了
                                       [self.dataArray addObjectsFromArray:result.msglist];
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                   }else{//还有更多数据
                                       self.page++;
                                       [self.dataArray addObjectsFromArray:result.msglist];
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshing];
                                       self.tableView.mj_footer.alpha = 0;
                                   }
                               }else if([result.code isEqualToString:@"400"]){
                                   [self.tableView.mj_header endRefreshing];
                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                   self.tableView.mj_footer.alpha = 1.0f;
                               }else{
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   [self.tableView.mj_footer endRefreshing];
                               }
                           } failure:^(NSError *err) {
                               [self.tableView.mj_footer endRefreshing];
                           }];
}

#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HXReachTableViewCell *cell = [HXReachTableViewCell cellWithTableView:tableView indexPath:indexPath data:self.dataArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(NSMutableArray<HXReachListModel *> *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

@end
